package xin.marcher.module.lifecycle.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.lifecycle.domain.dto.ItemPeriodStatusDTO;
import xin.marcher.module.lifecycle.domain.request.ItemPeriodStatusRequest;
import xin.marcher.module.lifecycle.domain.response.ItemPeriodStageResponse;
import xin.marcher.module.lifecycle.service.ItemPeriodStatusService;

/**
 *
 */
@Slf4j
@RestController
@RequestMapping("/ItemStatus")
public class ItemStatusController {

    @Autowired
    private ItemPeriodStatusService itemPeriodStatusService;

    /**
     * 批量获取商品状态信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/pageItemStatus")
    public JsonResult<PageResult<ItemPeriodStatusDTO>> pageItemStatus(ItemPeriodStatusRequest request) {
        try {
            PageResult<ItemPeriodStatusDTO> page = itemPeriodStatusService.pageItemStatus(request);
            return JsonResult.buildSuccess(page);
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 保存商品状态
     *
     * @param request
     * @return
     */
    @RequestMapping("/saveItemStatus")
    public JsonResult<ItemPeriodStageResponse> saveItemStatus(ItemPeriodStatusRequest request) {
        try {
            ItemPeriodStageResponse response = itemPeriodStatusService.saveItemStatus(request);
            return JsonResult.buildSuccess(response);
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 修改商品状态
     *
     * @param request
     * @return
     */
    @RequestMapping("/updateItemStatus")
    public JsonResult<ItemPeriodStageResponse> updateItemStatus(ItemPeriodStatusRequest request) {
        try {
            ItemPeriodStageResponse response = itemPeriodStatusService.updateItemStatus(request);
            return JsonResult.buildSuccess(response);
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 查询商品状态
     *
     * @param itemId
     * @return
     */
    @RequestMapping("/getItemStatus")
    public JsonResult<Integer> getItemStatus(String itemId) {
        try {
            Integer itemStatus = itemPeriodStatusService.getItemStatus(itemId);
            return JsonResult.buildSuccess(itemStatus);
        } catch (Exception e) {
            log.error("system error: request={}", itemId, e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
